﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CaiyinSoft.NetGather.Protocol;

namespace CaiyinSoft.NetGather.NetGatherLib.Filter
{
    [Serializable]
    public class XPathHTMLAnalyze : HTMLFilterBase
    {

        public override string FilterCaption
        {
            get
            {
                return "XPath过滤器";
            }
        }

        public override string ExpressionCaption
        {
            get
            {
                return "XPath";
            }
        }

        public override string Description
        {
            get
            {
                return "通过XPath过滤数据";
            }
        }

        public XPathHTMLAnalyze():base()
        { 
            _InitParams();
        }

        private void _InitParams()
        {
            SetInputHTML( "");
            OutputKey = Guid.NewGuid().ToString();
            //OutputType = 0;
            //ParseType = 0;
            FilterExpression = "";
        }


        /// <summary>
        /// 以指定方式解析HTML字符串，生成新字符串
        /// </summary>
        /// <returns></returns>
        public override List<FilterResult> FilterHTML()
        {
            return _XPathParseHTML();
        }

        private List<FilterResult> _XPathParseHTML()
        {
            int parentHTMLPosition = 0;
            if (ParentFilter != null)
                parentHTMLPosition = ParentFilter.HTMLPosition;
            HTMLPosition = parentHTMLPosition;

            List<FilterResult> parentResults = GetInputHTML();
            if (parentResults == null || parentResults.Count == 0) return null;
            List<FilterResult> listRevalue = new List<FilterResult>();

            HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
            foreach (var pr in parentResults)
            {
                if (string.IsNullOrEmpty(pr.HTMLResult) || pr.HTMLResult.Length == 0)
                    continue;

                if (FilterExpression.Trim().Length == 0)
                {
                    listRevalue.Add(pr);
                    continue;
                }

                doc.LoadHtml(pr.HTMLResult);
                HtmlAgilityPack.HtmlNodeCollection contents = doc.DocumentNode.SelectNodes(FilterExpression);
                if (contents != null)
                {
                    foreach (HtmlAgilityPack.HtmlNode content in contents)
                    {
                        HTMLPosition = Math.Max(HTMLPosition, parentHTMLPosition + content.StreamPosition);//  HTMLAnalyzer.GetPosition(content.Line, content.LinePosition));
                        //sb.AppendFormat("{0}", content.OuterHtml);
                        FilterResult result = new FilterResult();
                        result.HTMLPosition = pr.HTMLPosition + content.StreamPosition; // HTMLAnalyzer.GetPosition(content.Line, content.LinePosition);
                        result.HTMLResult = string.Format("{0}", content.OuterHtml);
                        listRevalue.Add(result);
                    }
                }
            }

            return listRevalue;
        }

    }
}
